from pathlib import Path
import json
import plotly.express as px
import pandas as pd

# 读取文本文件
path = Path('data/eq_data_m1.geojson')
content = path.read_text(encoding='utf8')
all_eq_data = json.loads(content)

# 按照格式输出
# wpath = Path('data/eq_data_format.geojson')
# readable = json.dumps(all_eq_data, indent=4)
# wpath.write_text(readable)

# 处理数据
all_eq = all_eq_data['features']
lons,lats,mags,titles = [],[],[],[]
for one_eq in all_eq:
    lons.append(one_eq['geometry']['coordinates'][0])
    lats.append(one_eq['geometry']['coordinates'][1])
    mags.append(one_eq['properties']['mag'])
    titles.append(one_eq['properties']['title'])

# 构造dataframe
data = pd.DataFrame(data=zip(lons,lats,titles,mags),columns=['经度','维度','标题','震级'])
data.head()

# 显示图像-散点图
fig = px.scatter(data, x='经度',y='维度',hover_name='标题',
                 size='震级',size_max=10,color='震级', color_continuous_scale='aggrnyl',
                 width=1600,height=1000,title='地震图')
fig.show()